////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.core.util
{
	import flash.utils.Dictionary;
	
	/**
	 * @private
	 * 
	 * This class is an extract from UIDUtil from flex
	 * */
	public class UIDUtil
	{
		/**
	     *  @private
	     *  Char codes for 0123456789ABCDEF
	     */
	    private static const ALPHA_CHAR_CODES:Array = [48, 49, 50, 51, 52, 53, 54, 
	        55, 56, 57, 65, 66, 67, 68, 69, 70];
	
	    //--------------------------------------------------------------------------
	    //
	    //  Class variables
	    //
	    //--------------------------------------------------------------------------
	
	    /** 
	     *  This Dictionary records all generated uids for all existing items.
	     */
	    private static var uidDictionary:Dictionary = new Dictionary(true);
	
	    //--------------------------------------------------------------------------
	    //
	    //  Class methods
	    //
	    //--------------------------------------------------------------------------
	
	    /**
	     *  Generates a UID (unique identifier) based on ActionScript's
	     *  pseudo-random number generator and the current time.
	     *
	     *  <p>The UID has the form
	     *  <code>"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"</code>
	     *  where X is a hexadecimal digit (0-9, A-F).</p>
	     *
	     *  <p>This UID will not be truly globally unique; but it is the best
	     *  we can do without player support for UID generation.</p>
	     *
	     *  @return The newly-generated UID.
	     */
	    public static function createUID():String
	    {
	        var uid:Array = new Array(36);
	        var index:int = 0;
	        
	        var i:int;
	        var j:int;
	        
	        for (i = 0; i < 8; i++)
	        {
	            uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
	        }
	
	        for (i = 0; i < 3; i++)
	        {
	            uid[index++] = 45; // charCode for "-"
	            
	            for (j = 0; j < 4; j++)
	            {
	                uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
	            }
	        }
	        
	        uid[index++] = 45; // charCode for "-"
	
	        var time:Number = new Date().getTime();
	        // Note: time is the number of milliseconds since 1970,
	        // which is currently more than one trillion.
	        // We use the low 8 hex digits of this number in the UID.
	        // Just in case the system clock has been reset to
	        // Jan 1-4, 1970 (in which case this number could have only
	        // 1-7 hex digits), we pad on the left with 7 zeros
	        // before taking the low digits.
	        var timeString:String = ("0000000" + time.toString(16).toUpperCase()).substr(-8);
	        
	        for (i = 0; i < 8; i++)
	        {
	            uid[index++] = timeString.charCodeAt(i);
	        }
	        
	        for (i = 0; i < 4; i++)
	        {
	            uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
	        }
	        
	        return String.fromCharCode.apply(null, uid);
	    }
	}
}